package com.clkj.xiuzheng.controller.shangcheng;

import com.clkj.xiuzheng.pojo.common.Result;
import com.clkj.xiuzheng.pojo.common.StatusCode;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCategory;
import com.clkj.xiuzheng.pojo.shangcheng.ProductCategoryRequest;
import com.clkj.xiuzheng.service.shangcheng.ScCategoryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ScCategory")
@CrossOrigin
@Api(value = "/ScCategory", tags = {"Z商城分类管理"})
public class ScCategoryController {

    @Autowired
    private ScCategoryService scCategoryService;

    @ApiOperation(value = "根据父类id(和公司id<可以不填>)获取药品类别信息")
    @PostMapping("/getProductCategory")
    public Result getProductCategory( @ApiParam(name = "parentId", value = "药品父类id") @RequestParam Integer parentId,
                                      @ApiParam(name = "companyId", value = "公司ID")  @RequestParam(required = false) Integer companyId) {
        try {
            List<ProductCategory> list =  scCategoryService.getProductCategory(parentId,companyId);
            return new Result(true, StatusCode.OK,"查询成功",list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"查询失败");
        }
    }
    @ApiOperation(value = "根据类别id批量下架药品类别")
    @PostMapping("/DownCategory")
    public Result DownCategory(@ApiParam(name = "CategoryIdList",value ="父级id字符串 ,号间隔") @RequestParam("CategoryIdList") String CategoryIdList) {
        try {
            scCategoryService.DownCategory(CategoryIdList);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
    @ApiOperation(value ="添加药物类别(以及分类parentId=0)")
    @PostMapping(value="/addFirstCategory")
    public Result addFirstCategory(ProductCategoryRequest categoryList, HttpServletRequest request) {

        try {
            scCategoryService.addFirstCategory(categoryList,request);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }

    @ApiOperation(value ="添加药物类别(二级分类)")
    @PostMapping("/addCategory")
    public Result addCategory(@ApiParam(name = "parentId",value ="父级id") @RequestParam("parentId") Integer parentId,
                              @ApiParam(name = "categoryNames",value = "分裂名集合字符串用,分割") @RequestParam("categoryNames") String categoryNames,
                              @ApiParam(name = "flag",value ="0一级，1二级 2三级") @RequestParam("flag") Integer flag,
                              @ApiParam(name = "companyId",value ="公司id") @RequestParam("companyId") Integer companyId) {

            try {
                scCategoryService.addCategory(parentId,categoryNames,flag,companyId);
                return new Result(true, StatusCode.OK,"修改成功");
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, StatusCode.ERROR,"修改失败");
            }
}
    @ApiOperation(value = "根据id修改类别名称")
    @PostMapping("/updateCategoryName")
    public Result updateCategoryName(@ApiParam(name = "categoryId", value = "类别id") @RequestParam("categoryId") Integer categoryId,
                                     @ApiParam(name = "categoryName", value = "类别名称") @RequestParam("categoryName") String categoryName) {
        try {
            scCategoryService.updateCategoryName(categoryName,categoryId);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }
    @ApiOperation(value = "根据id字符串批量上下架别名称")
    @PostMapping("/updateCategoryStatus")
    public Result updateCategoryStatus(@ApiParam(name = "Ids", value = "类别id集合字符串用,分割") @RequestParam("Ids") String Ids,
                                     @ApiParam(name = "status", value = "0下架 1上架") @RequestParam("status") Integer status) {
        try {
            scCategoryService.updateCategoryStatus(Ids,status);
            return new Result(true, StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"修改失败");
        }
    }

    @ApiOperation(value = "获取小程序商品分类页面")
    @PostMapping("/listCategory")
    public Result listCategory(@ApiParam(name = "firstId", value = "一级分类id，全部传0") @RequestParam("firstId") Integer firstId ,
                               @ApiParam(name = "secId", value = "二级分类id，默认传0") @RequestParam("secId") Integer secId ,
                               @ApiParam(name = "brandId", value = "品牌id,默认为0") @RequestParam("brandId") Integer brandId) {
        try {
            Map map = scCategoryService.listCategory(firstId,secId, brandId);
            return new Result(true, StatusCode.OK, "查询成功",map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "查询失败");
        }
    }



}
